<%@ page import="java.util.List" %>
<%@ page import="beans.StuInfoBean" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>StuFrame</title>
    <link rel="shortcut icon" href="./favicon.png">
    <link rel="stylesheet" type="text/css" href="pageStyle.css">
    <style>
        .content {
            background-color: inherit;
        }
    </style>
</head>
<body>
<div class="back">
    <div class="content">

        <label for="searchType">
            <select id="searchType" name="searchType">
                <option value="search_college">按学院查找：</option>
                <option value="search_major">按专业查找：</option>
                <option value="search_username">按姓名查找：</option>
                <option value="search_id">按学号查找：</option>
            </select>
        </label>
        <label for="search"><input type="text" id="search" name="search"></label>
        <input type="button" id="btn_search" name="btn_search" value="查询">

    </div>
    <script>
        btn_search = document.getElementById("btn_search");
        btn_search.onclick = function () {
            let searchType_str = document.getElementById("searchType").value;
            let search = document.getElementById("search").value;
            let search_str = search.trim();

            if (search_str != null && search_str !== "") {
                //alert("searchType:" + searchType_str + "  search:" + search_str);
                const xhr = new XMLHttpRequest();
                let url = "./stuGetInfo.do?searchType=" + searchType_str + "&search=" + search_str + "&r=" + Math.random();
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4 && xhr.status === 200) {
                        if (xhr.responseText === "true")
                            location.reload();
                        else
                            alert("未查询到信息");
                    }
                }
                xhr.open("GET", url, true);
                xhr.send();
            }
        }
    </script>

    <%
        if (session.getAttribute("stuList") != null) {
            List<StuInfoBean> list = (List<StuInfoBean>) session.getAttribute("stuList");
            out.print("<table class='info-table'><tr><th>学号</th><th>姓名</th><th>学院</th><th>专业</th><th>班级</th></tr>");
            for (StuInfoBean stu : list) {
                out.print("<tr>");
                out.print("<td>" + stu.getId() + "</td>");
                out.print("<td>" + stu.getUsername() + "</td>");
                out.print("<td>" + stu.getCollege() + "</td>");
                out.print("<td>" + stu.getMajor() + "</td>");
                out.print("<td>" + stu.getOfClass() + "</td>");
                out.print("</tr>");
            }
            out.print("</table>");
        } else if (session.getAttribute("stuInfo") != null) {
            StuInfoBean stu = (StuInfoBean) session.getAttribute("stuInfo");
            out.print("<table class='info-table'><tr><th>学号</th><th>姓名</th><th>学院</th><th>专业</th><th>班级</th></tr>");
            out.print("<tr>");
            out.print("<td>" + stu.getId() + "</td>");
            out.print("<td>" + stu.getUsername() + "</td>");
            out.print("<td>" + stu.getCollege() + "</td>");
            out.print("<td>" + stu.getMajor() + "</td>");
            out.print("<td>" + stu.getOfClass() + "</td>");
            out.print("</tr></table>");
        }
    %>

</div>
</body>
</html>
